<?php
namespace Plu\Library\Services\Report\LogRecord;

use Plu\Library\Services\ServiceBase,
    Plu\Library\Models\Report\LogRecord\LogOperationTypes as LTypes,
    Plu\Library\Models\Report\LogRecord\LogOperationRecord as LRecord;


class LogRecordService extends ServiceBase {

    const COLOR_NORMAL_OPERATE  = 0;

    const COLOR_ADD_OPERATE     = 1;

    const COLOR_REMOVE_OPERATE  = 2;

    const COLOR_KEY_OPERATE     = 3;

    private $cache_key;

    private $cache;

    const CACHE_TIME = 86400;

    const KEY_OF_CACHE = "log_record_%s_houtai";

    //logtype对应的是自己的项目名称，比如app后台 logTypeName = "app";
    public function __construct($logTypeName, $typeConfig) {
        $this->cache = $this->getCacheHandler();
        $this->cache_key = sprintf(self::KEY_OF_CACHE,$logTypeName);
        $lastConfig = $this->cache->get($this->cache_key);
        if(empty($lastConfig)){
            $all_config = $this->addItem($typeConfig);
            $this->cache->save($this->cache_key,$all_config,self::CACHE_TIME);
        }else{
            $lastTypeConfig = array();
            foreach($lastConfig as $key=>$value){
                $lastTypeConfig[$key]['name'] = $value['name'];
                $lastTypeConfig[$key]['desc'] = $value['desc'];
                $lastTypeConfig[$key]['moduleId'] = $value['moduleId'];
                $lastTypeConfig[$key]['color'] = $value['color'];
            }
            $is_diff = false;
            foreach($typeConfig as $k=>$v){
                if(isset($lastTypeConfig[$k])){
                    $compare = array_diff_assoc($v,$lastTypeConfig[$k]);
                    if(!empty($compare)){
                        $is_diff = true;
                    }
                }else{
                    $is_diff = true;
                }
            }
            if ($is_diff){
                $diffLogConfig = $this->addItem($typeConfig);
                $all_config = array_merge($lastConfig,$diffLogConfig);
                $this->cache->save($this->cache_key,$all_config,self::CACHE_TIME);
            }
        }
    }

    public function addItem($types) {
        $items = [];
        foreach($types as $k => $value) {
            $isExist = LTypes::getItem($value['name'],$value['moduleId'],$value['color']);
            if($isExist === false){
                $t = new LTypes();
                $t->Name = $value['name'];
                $t->Desc = $value['desc'];
                $t->ModuleId = $value['moduleId'];
                $t->Color = $value['color'];
                $t->save();
                $id = $t->Id;
            }else{
                $item = $isExist->toArray();
                $id = $item['Id'];
            }
            $items[$k] = [
                'name'     => $value['name'],
                'desc'     => $value['desc'],
                'moduleId' => $value['moduleId'],
                'color'    => $value['color'],
                'type'     => $id
            ];
        }
        return $items;
    }

    public function logger($type, $detail, $target="", $result="", $ready1="", $ready2="", $ready3=""){
        $logCacheConfig = $this->cache->get($this->cache_key);
        $currentTypeConfig = $logCacheConfig[$type];
        $userId = -1;
        if(!empty($currentTypeConfig)){
            $logRecord = new LRecord();
            $logRecord->ModuleId = $currentTypeConfig['moduleId'];
            $logRecord->UserId   = $userId;
            $logRecord->Type     = $currentTypeConfig['type'];
            $logRecord->Detail   = $detail;
            if(!empty($target)){
                $logRecord->Target = $target;
            }
            if(!empty($result)){
                $logRecord->Result = $result;
            }
            if(!empty($ready1)){
                $logRecord->Ready1 = $ready1;
            }
            if(!empty($ready2)){
                $logRecord->Ready2 = $ready2;
            }
            if(!empty($ready3)){
                $logRecord->Ready3 = $ready3;
            }
            $logRecord->CreateTime = date("Y-m-d H:i:s");
            return $logRecord->save();
        }else{
            return false;
        }
    }
}
?>
